﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WMS.API.Application.Command.SystemUser;

namespace WMS.API.Controllers.SystemUser
{
    /// <summary>
    /// 财务明细控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FinancialdetailsController : ControllerBase
    {
        /// <summary>
        /// MediatR服务，用于发送命令到对应的处理程序
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造函数:初始化财务明细控制器
        /// </summary>
        /// <param name="mediator">MediatR服务，用于发送命令到对应的处理程序</param>
        public FinancialdetailsController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取财务明细
        /// </summary>
        /// <param name="currentPage">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="searchField">搜索字段</param>
        /// <param name="searchValue">搜索值</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>财务明细列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetFinancialdetails(
            [FromQuery] int currentPage = 1,
            [FromQuery] int pageSize = 10,
            [FromQuery] string? searchField = null,
            [FromQuery] string? searchValue = null,
            [FromQuery] DateTime? startTime = null,
            [FromQuery] DateTime? endTime = null)
        {
            var query = new GetFinancialdetailsQuery
            {
                CurrentPage = currentPage,
                PageSize = pageSize,
                SearchField = searchField,
                SearchValue = searchValue,
                StartTime = startTime,
                EndTime = endTime
            };
            return Ok(await mediator.Send(query));
        }

        /// <summary>
        /// 导出财务明细为Excel
        /// </summary>
        /// <param name="searchField">搜索字段</param>
        /// <param name="searchValue">搜索值</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>Excel文件流</returns>
        [HttpGet]
        public async Task<IActionResult> ExportFinancialdetails(
            [FromQuery] string? searchField = null,
            [FromQuery] string? searchValue = null,
            [FromQuery] DateTime? startTime = null,
            [FromQuery] DateTime? endTime = null)
        {
            var query = new ExportFinancialdetailsQuery
            {
                SearchField = searchField,
                SearchValue = searchValue,
                StartTime = startTime,
                EndTime = endTime
            };

            var result = await mediator.Send(query);

            if (result.Code == WMS.ErrorCode.ResultCode.Success && result.Data != null)
            {
                return File(result.Data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "财务明细.xlsx");
            }
            else
            {
                return BadRequest(result.Message);
            }
        }
    }
}
